<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\MerchantWeixin;
use app\modules\v1\models\Merchant;
use app\modules\v1\models\Merchantinfocard;
use app\modules\v1\models\PaymentCode;
use yii\base\Exception;
use Qiniu\json_decode;
use app\modules\v1\models\Kameng;
use app\modules\v1\models\ThreeElementsQueryLog;

class PostidentityController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\cashdesk';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid 	= isset(Yii::$app->request->post()['openid'])?trim(Yii::$app->request->post()['openid']):'';
    		$name 		= isset(Yii::$app->request->post()['name'])?trim(Yii::$app->request->post()['name']):'';
    		$id_card 	= isset(Yii::$app->request->post()['id_card'])?trim(Yii::$app->request->post()['id_card']):'';
    		$phone 		= isset(Yii::$app->request->post()['phone'])?trim(Yii::$app->request->post()['phone']):'';
    		$phone_code = isset(Yii::$app->request->post()['phone_code'])?Yii::$app->request->post()['phone_code']:'';
    		$merchant_name 		= isset(Yii::$app->request->post()['merchant_name'])?trim(Yii::$app->request->post()['merchant_name']):'';
    		$address_province 	= isset(Yii::$app->request->post()['address_province'])?trim(Yii::$app->request->post()['address_province']):'';
    		$address_city 		= isset(Yii::$app->request->post()['address_city'])?trim(Yii::$app->request->post()['address_city']):'';
    		$address_area 		= isset(Yii::$app->request->post()['address_area'])?trim(Yii::$app->request->post()['address_area']):'';
    		$address_detail 	= isset(Yii::$app->request->post()['address_detail'])?trim(Yii::$app->request->post()['address_detail']):'';
    		$bank_no 		= isset(Yii::$app->request->post()['bank_no'])?trim(Yii::$app->request->post()['bank_no']):'';
    		$bank_name 		= isset(Yii::$app->request->post()['bank_name'])?trim(Yii::$app->request->post()['bank_name']):'';
    		$bank_branch	= isset(Yii::$app->request->post()['bank_branch'])?trim(Yii::$app->request->post()['bank_branch']):'';
    		$bank_province 	= isset(Yii::$app->request->post()['bank_province'])?trim(Yii::$app->request->post()['bank_province']):'';
    		$bank_city 		= isset(Yii::$app->request->post()['bank_city'])?trim(Yii::$app->request->post()['bank_city']):'';
    		$bank_area 		= isset(Yii::$app->request->post()['bank_area'])?trim(Yii::$app->request->post()['bank_area']):'';
    		$bank_card_type = isset(Yii::$app->request->post()['bank_card_type'])?trim(Yii::$app->request->post()['bank_card_type']):'';
    		$bank_code 		= isset(Yii::$app->request->post()['bank_code'])?trim(Yii::$app->request->post()['bank_code']):'';
    		$merchant_no 	= isset(Yii::$app->request->post()['merchant_no'])?trim(Yii::$app->request->post()['merchant_no']):'';
    		if(empty($openid)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号';
    			$response			= Yii::$app->response;
    			$response->format 	= Response::FORMAT_JSON;
    			$response->data	  	= $res;
    			return $response;
    		}
    		if(empty($name)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '姓名不能为空';
    			$response			= Yii::$app->response;
    			$response->format 	= Response::FORMAT_JSON;
    			$response->data	  	= $res;
    			return $response;
    		}
    		if(empty($id_card)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '身份证号不能为空';
    			$response			= Yii::$app->response;
    			$response->format 	= Response::FORMAT_JSON;
    			$response->data	  	= $res;
    			return $response;
    		}
    		if(empty($phone)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '手机号不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		if(empty($merchant_name)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '商户名称不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		if(empty($address_province) || empty($address_city) || empty($address_area)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '商户地址不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		if(empty($address_detail)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '详细地址不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		
    		if(empty($bank_no)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '银行卡号不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		
    		if(empty($bank_name)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '开户银行不能为空';
    			$response			= Yii::$app->response;
    			$response->format 	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		
    		if(empty($bank_city) || empty($bank_area) || empty($bank_province)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '开户行地区不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		if(empty($bank_branch)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '开户支行不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		if(empty($bank_card_type)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '卡类型不能为空';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
     		if(empty($bank_code)){
     			$carfinfo1 = new Merchantinfocard();
     			$bank_code = $carfinfo1::GetBankCode($bank_name, $bank_province, $bank_city, $bank_area); 			
    		}
    		//用户微信数据
    		$weixininfo = MerchantWeixin::findByOpenid($openid);
    		if(empty($weixininfo)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    			exit;
    		}else{
    			//3要素验证
    			$threeelement = new ThreeElementsQueryLog();
    			$res_three = $threeelement->CheckThree($name, $id_card, $bank_no, $openid);
    			$tr = Yii::$app->db->beginTransaction();
    			try {
    				$merchant1 = Merchant::findOne($weixininfo->merchant_id);
    				if(empty($merchant1)){
    					$res['code'] 		= '10402';
    					$res['msg']  		= '请取消关注后重新关注公众号';
    					$response			= Yii::$app->response;
    					$response->format	= Response::FORMAT_JSON;
    					$response->data		= $res;
    					return $response;
    					exit;
    				}
    				//实体码上面的编码
    				if(!empty($merchant_no)){
    					if($merchant_no != $merchant1->merchant_no){
    						$check_string	= substr($merchant_no,0,2);
    						if($check_string == '01'){
    							$paymentcode = PaymentCode::findOne(['merchant_no' => $merchant_no]);
    							if(!empty($paymentcode)){
    								if($paymentcode->is_bind == '1'){
    									$res['code'] 		= '10402';
    									$res['msg']  		= '商户编号已被使用';
    									$response			= Yii::$app->response;
    									$response->format	= Response::FORMAT_JSON;
    									$response->data		= $res;
    									return $response;
    									exit;
    								}else{
    									//未绑定
    									$paymentcode->is_bind 	= 1;
    									$paymentcode->update();
    									$merchant1->qr_code   	= $paymentcode->id;
    									$merchant1->merchant_no = $merchant_no;
    								}
    							}else{
    								$res['code'] 		= '10402';
    								$res['msg']  		= '商户编号不存在';
    								$response			= Yii::$app->response;
    								$response->format	= Response::FORMAT_JSON;
    								$response->data		= $res;
    								return $response;
    								exit;
    							}
    						}else{
    							$res['code'] 		= '10402';
    							$res['msg']  		= '商户编号不存在';
    							$response			= Yii::$app->response;
    							$response->format	= Response::FORMAT_JSON;
    							$response->data		= $res;
    							return $response;
    							exit;
    						}
    					}
    				}else{
    					//为空
    					$next_id 			= PaymentCode::find()->max('id')+1;
    					$merchant_no_new  	= PaymentCode::getMerchantNo(6);
    					$newpaymentcode   	= new PaymentCode();
    					$newpaymentcode->merchant_no= $merchant_no_new;
    					$newpaymentcode->is_bind    = 1;
    					$newpaymentcode->id     	= $next_id;
    					$newpaymentcode->save();
    					$merchant1->qr_code   		= $next_id;
    					$merchant1->merchant_no 	= $merchant_no_new;
    				}
    				$merchant1->pwd_login 			= md5(rand(100000, 999999));
    				$merchant1->mobile 				= $phone;
    				$merchant1->identity_card 		= $id_card;
    				$merchant1->merchant_name 		= $merchant_name;
    				$merchant1->merchant_real_name 	= $name;
    				$merchant1->province 			= $address_province;
    				$merchant1->city 				= $address_city;
    				$merchant1->county 				= $address_area;
    				$merchant1->detail_address 		= $address_detail;
    				$merchant1->reg_time 			= date("Y-m-d H:i:s",time()+8*3600);
    				$merchant3 = Merchantinfocard::findOne(['merchant_id'=>$weixininfo->merchant_id]);
    				if(empty($merchant3)){
    					$merchant2 = new Merchantinfocard();
    					$merchant2->merchant_id 		= $weixininfo->merchant_id;
    					$merchant2->account_no  		= $bank_no;
    					$merchant2->bank_name 			= $bank_name;
    					$merchant2->bank_no 			= $bank_name;  
    					$merchant2->bank_card_type 		= $bank_card_type;
    					$merchant2->bank_province 		= $bank_province;
    					$merchant2->bank_city 			= $bank_city;
    					$merchant2->bank_county 		= $bank_area;
    					$merchant2->bank_branch 		= $bank_branch;
    					$merchant2->bank_code 			= $bank_code;
    					if($res_three['code'] == '10000'){
    						$merchant2->state 			= 1;
    						$merchant2->is_bind 		= 1;
    						$merchant2->create_time_success =  date("Y-m-d H:i:s",time()+8*3600);
    						$map = array();
    						$wxT0Fee		= Yii::$app->params['wx_rates'];
    						$wxT1Fee		= Yii::$app->params['wx_rates'];
    						$alipayT0Fee 	= Yii::$app->params['zfb_rates'];
    						$alipayT1Fee 	= Yii::$app->params['zfb_rates'];
    						$map['accountName'] = $name;
    						$map['accountNo'] 	= $bank_no;
    						$map['alipayT0Fee'] = (string)$alipayT0Fee;
    						$map['alipayT1Fee'] = (string)$alipayT1Fee;
    						$map['alipayType'] 	= (string)Yii::$app->params['alipayType'];
    						$map['bankBranch'] 	= $bank_branch;
    						$map['bankCity'] 	= $bank_city;
    						$map['bankCode'] 	= $bank_code;
    						$map['bankName'] 	= $bank_name;
    						$map['bankProv'] 	= $bank_province;
    						$map['bankType'] 	= (string)Yii::$app->params['bankType'];
    						$map['channelName'] = (string)Yii::$app->params['channelname'];
    						$map['channelNo'] 	= (string)Yii::$app->params['channelno'];
    						$map['installCity'] = $address_city;
    						$map['installCounty'] 		= $address_area;
    						$map['installProvince'] 	= $address_province;
    						$map['legalPersonID'] 		= $id_card;
    						$map['legalPersonName'] 	= $name;
    						$map['merchantBillName'] 	= $merchant_name;   //签购单显示名称
    						$map['merchantName'] 		= $merchant_name;
    						$map['merchantPersonName'] 	= $name; //商户联系人姓名
    						$map['merchantPersonPhone'] = $phone;
    						$map['merchantType'] 		= (string)Yii::$app->params['merchantType'];
    						$map['operateAddress'] 		= $address_detail;
    						$map['remarks'] 			= '描述';
    						$map['wxT0Fee'] 			= (string)$wxT0Fee;
    						$map['wxT1Fee'] 			= (string)$wxT1Fee;
    						$map['wxType'] 				= (string)Yii::$app->params['wxType'];
    						$res11 = Kameng::MerchantIn($map);
    						if(!empty($res11)){
    							//进件次数保存
    							$merchant1->jinjian_time = 1;
    							$res11 = json_decode($res11);
    							//成功后保存数据
    							if(isset($res11->respCode) && $res11->respCode == '0000'){
    								$merchant1->sign_key 			 = $res11->signKey;
    								$merchant1->des_key 			 = $res11->desKey;
    								$merchant1->query_key			 = $res11->queryKey;
    								$merchant1->vnion_merchant_no 	 = $res11->merchantNo;
    								$merchant1->check_status_ischeck = 1;
    								$merchant1->jinjian_info 		 = $res11->respMsg;
    								$merchant1->check_status_jinjian = 3;
    								$merchant1->check_status 		 = 6;
    								$merchant1->is_enable 			 = 8;
    								$merchant1->free_balance 		 = Yii::$app->params['free_balance'];
    								$sendWeixin = new Merchant();
    								$sendWeixin->WeixinRenzhengOk($name, $openid);
    							}else{
    								$merchant1->check_status_ischeck 	 = 1;
    								$merchant1->jinjian_info 			 = $res11->respMsg;
    								$merchant1->check_status_jinjian 	 = 2;
    								$sendWeixin = new Merchant();
    								$sendWeixin->WeixinRenzhengFail($name, $openid,$res11->respMsg);
    							}
    						}else{
    							$merchant1->check_status_ischeck 	 = 1;
    							$merchant1->check_status_jinjian 	 = 2;
    							$merchant1->jinjian_info 			 = $res11->respMsg;
    							$sendWeixin = new Merchant();
    							$sendWeixin->WeixinRenzhengFail($name, $openid, $res11->respMsg);
    						}
    					}else{
    						$res['code'] 		= '10400';
    						$res['msg']  		= '身份证号、姓名、银行卡号不一致';
    						$response			= Yii::$app->response;
    						$response->format	= Response::FORMAT_JSON;
    						$response->data		= $res;
    						return $response;
    						exit;
    					}
    					$merchant2->create_time 		=  date("Y-m-d H:i:s",time()+8*3600);
    					$merchant2->vnion_status 		= 1;
    					$merchant2->signle_time_amount 	= Yii::$app->params['signle_time_amount'];
    					$merchant2->signle_day_amount 	= Yii::$app->params['signle_day_amount'];
    					$merchant2->save();
    				}else{
    					if (!empty($merchant1->vnion_merchant_no) && ($merchant1->check_status_jinjian == 3)){
    						$res['code'] 		= '10400';
    						$res['msg']  		= '商户认证成功，不能修改信息';
    						$response	 		= Yii::$app->response;
    						$response->format	= Response::FORMAT_JSON;
    						$response->data		= $res;
    						return $response;
    						exit;
    					}
    					if(($merchant1->jinjian_time)>5){
    						$res['code'] 		= '10400';
    						$res['msg']  		= '该账号认证次数超限。';
    						$response	 		= Yii::$app->response;
    						$response->format	= Response::FORMAT_JSON;
    						$response->data		= $res;
    						return $response;
    						exit;
    					}
    					//检查输入的信息是否重复提交
    					if(($merchant3->account_no == $bank_no) && ($merchant3->bank_name == $bank_name) && 
    							($merchant3->bank_card_type == $bank_card_type) && ($merchant3->bank_province == $bank_province) &&
    							($merchant3->bank_city == $bank_city) && ($merchant3->bank_county == $bank_area) &&
    							($merchant3->bank_branch == $bank_branch) && ($merchant3->bank_code == $bank_code)){
    						$res['code'] 		= '10400';
    						$res['msg']  		= '信息已提交,等待审核';
    						$response	 		= Yii::$app->response;
    						$response->format	= Response::FORMAT_JSON;
    						$response->data		= $res;
    						return $response;
    						exit;
    					}
    					$merchant3->account_no  		= $bank_no;
    					$merchant3->bank_name 			= $bank_name;
    					$merchant3->bank_no 			= $bank_name;
    					$merchant3->bank_card_type 		= $bank_card_type;
    					$merchant3->bank_province 		= $bank_province;
    					$merchant3->bank_city 			= $bank_city;
    					$merchant3->bank_county 		= $bank_area;
    					$merchant3->bank_branch 		= $bank_branch;
    					$merchant3->bank_code 			= $bank_code;
    					if($res_three['code'] == '10000'){
    						$merchant3->state 				= 1;
    						$merchant3->is_bind 			= 1;
    						$merchant3->create_time_success =  date("Y-m-d H:i:s",time()+8*3600);
    						//三要素验证通过，直接进件
    						$map = array();
    						$wxT0Fee	=  Yii::$app->params['wx_rates'];
    						$wxT1Fee	=  Yii::$app->params['wx_rates'];
    						$alipayT0Fee 	= Yii::$app->params['zfb_rates'];
    						$alipayT1Fee 	= Yii::$app->params['zfb_rates'];
    						$map['accountName'] = $name;
    						$map['accountNo'] 	= $bank_no;
    						$map['alipayT0Fee'] = (string)$alipayT0Fee;
    						$map['alipayT1Fee'] = (string)$alipayT1Fee;
    						$map['alipayType'] 	= (string)Yii::$app->params['alipayType'];
    						$map['bankBranch'] 	= $bank_branch;
    						$map['bankCity'] 	= $bank_city;
    						$map['bankCode'] 	= $bank_code;
    						$map['bankName'] 	= $bank_name;
    						$map['bankProv'] 	= $bank_province;
    						$map['bankType'] 	= (string)Yii::$app->params['bankType'];
    						$map['channelName'] = (string)Yii::$app->params['channelname'];
    						$map['channelNo'] 	= (string)Yii::$app->params['channelno'];
    						$map['installCity'] = $address_city;
    						$map['installCounty'] 		= $address_area;
    						$map['installProvince'] 	= $address_province;
    						$map['legalPersonID'] 		= $id_card;
    						$map['legalPersonName'] 	= $name;
    						$map['merchantBillName'] 	= $merchant_name;   //签购单显示名称
    						$map['merchantName'] 		= $merchant_name;
    						$map['merchantPersonName'] 	= $name; //商户联系人姓名
    						$map['merchantPersonPhone'] = $phone;
    						$map['merchantType'] 		= (string)Yii::$app->params['merchantType'];
    						$map['operateAddress'] 		= $address_detail;
    						$map['remarks'] 			= '描述';
    						$map['wxT0Fee'] 			= (string)$wxT0Fee;
    						$map['wxT1Fee'] 			= (string)$wxT1Fee;
    						$map['wxType'] 				= (string)Yii::$app->params['wxType'];
    						$res12 = Kameng::MerchantIn($map);
    						if(!empty($res12)){
    							$merchant1->jinjian_time			= ($merchant1->jinjian_time)+1;
    							$res12 = json_decode($res12);
    							//成功后保存数据
    							if(isset($res12->respCode) && $res12->respCode == '0000'){
    								$merchant1->sign_key 			 = $res12->signKey;
    								$merchant1->des_key 			 = $res12->desKey;
    								$merchant1->query_key			 = $res12->queryKey;
    								$merchant1->vnion_merchant_no 	 = $res12->merchantNo;
    								$merchant1->check_status_ischeck = 1;
    								$merchant1->jinjian_info 		 = $res12->respMsg;
    								$merchant1->check_status_jinjian = 3;
    								$merchant1->check_status 		 = 6;
    								$merchant1->is_enable 			 = 8;
    								$merchant1->free_balance 		 = Yii::$app->params['free_balance'];
    								$sendWeixin = new Merchant();
    								$sendWeixin->WeixinRenzhengOk($name, $openid);
    							}else{
    								$merchant1->jinjian_info = $res12->respMsg;
    								$sendWeixin = new Merchant();
    								$sendWeixin->WeixinRenzhengFail($name, $openid,$res12->respMsg);	
    							}
    						}
    					}else{
    						$res['code'] 		= '10400';
    						$res['msg']  		= '身份证号、姓名、银行卡号不一致';
    						$response			= Yii::$app->response;
    						$response->format	= Response::FORMAT_JSON;
    						$response->data		= $res;
    						return $response;
    					}
    					$merchant3->create_time 		=  date("Y-m-d H:i:s",time()+8*3600);
    					$merchant3->vnion_status 		= 1;
    					$merchant3->signle_time_amount 	= Yii::$app->params['signle_time_amount'];
    					$merchant3->signle_day_amount 	= Yii::$app->params['signle_day_amount'];
    					$merchant3->update();
    				}
    				$merchant1->update();
    				//提交
    				$tr->commit();
    				$res['code'] 		= '10000';
    				$res['msg']  		= '成功';
    				$response			= Yii::$app->response;
    				$response->format	= Response::FORMAT_JSON;
    				$response->data		= $res;
    				return $response;
    			} catch (Exception $e) {
    				//回滚
    				$tr->rollBack();
    				$res['code'] 		= '10400';
    				$res['msg']  		= '保存失败';
    				$response			= Yii::$app->response;
    				$response->format	= Response::FORMAT_JSON;
    				$response->data		= $res;
    				return $response;
    			}
    		}
    	}else{
    		$res['code'] 		= '10402';
    		$res['msg']  		= '请取消关注后重新关注公众号';
    		$response			= Yii::$app->response;
    		$response->format	= Response::FORMAT_JSON;
    		$response->data		= $res;
    		return $response;
    	}
    }

}